'Annual Report Requirement Script  
Option Strict Off
imports System
imports System.Collections
imports Humans = PHSRAG.Insight21.Humans
imports PHSRAG.Insight21.BusinessObjects
imports FormsManagement = PHSRAG.Insight21.FormsManagement

module Script
sub Main()

    Dim hazardExists as Boolean
    for each qa as FormsManagement.QuestionnaireAnswer in currentProtVersion.QuestionnaireAnswers
        if qa.QuestionnaireCategory = "QUESTCAT_ANM_HZ" then
            hazardExists = true
        end if
    next

    Dim answers as System.Collections.ArrayList = currentProtVersion.QuestionnaireAnswers
    if answers.Count > 0 Then
        Dim latestIRVersion as Humans.ProtocolVersion = Humans.Protocol.GetLatestApplicationVersion (theProtocol.Id, connectionString, false, cachePolicy)

        if not latestIRVersion is nothing then
            AddForm("AskHazards", "Y", "Hazardous Agent Administration and Use", hazardExists, latestIRVersion.QuestionnaireAnswers, true)
        else
            Dim valueMatches as boolean
            AddForm("AskHazards", "Y", "Hazardous Agent Administration and Use", hazardExists, valueMatches)
        end if

        theProtocol.Save( connectionString )
        currentProtVersion.Save ( connectionString )
        currentProcess.Save( connectionString )
    end if
end sub  

' Adds or removes questionnaire answers to protocol version based on answer to a question, also copies previous forms
sub AddForm(byval answerName as string, byval answerValue as string, byval questionnaireName as string, byval formExists as boolean, byval oldAnswers as ArrayList, byval isMultiple as boolean)
    Dim answers as ArrayList = currentProtVersion.QuestionnaireAnswers(0).GetAnswers(answerName)
    if answers.Count > 0 then
        if answers(0).Value = answerValue and not formExists then
            currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, questionnaireName, questionnaireName, oldAnswers, isMultiple, theProtocol.Id)
        else if answers(0).Value <> answerValue
            currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
        end if
    else
            currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
    end if
end sub

' Adds or removes questionnaire answers to protocol version based on answer to a question
sub AddForm(byval answerName as string, byval answerValue as string, byval questionnaireName as string, byval formExists as boolean, byref valueMatches as boolean)
    Dim answers as Arraylist = currentProtVersion.QuestionnaireAnswers(0).GetAnswers( answerName )
    if answers.Count > 0 then
        if answers(0).Value = answerValue and not formExists then
            valueMatches = true
            Dim qv as FormsManagement.QuestionnaireVersion = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, questionnaireName, FormsManagement.Constants.QuestionnaireType.Animals )
            Dim qa as FormsManagement.QuestionnaireAnswer = currentProtVersion.AddNewQuestionnaireAnswer(qv.Id, cachePolicy, questionnaireName, not qv.Questionnaire.AllowMultiple)
        else if answers(0).Value = answerValue
            valueMatches = true
        else if answers(0).Value <> answerValue then
            valueMatches = false
            currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
        end if 
    else
        valueMatches = false
        currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
    end if
end sub

end module
